package org.jetpac.chatscript.client;

import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.ScrollPanel;

public class ChatPanel extends Composite {

	private final ScrollPanel scrollPanel = new ScrollPanel();
	private  FlexTable flexTable = new FlexTable();
	
	private static int rowNumbers = 0;
	
	public ChatPanel() {
		
		flexTable.setBorderWidth(0);
		flexTable.setCellPadding(0);
		flexTable.setCellSpacing(0);
		scrollPanel.add(flexTable);
		setRelativeHeight();
		initWidget(scrollPanel);
	}
	
	public void addString (String textToAdd) {
		flexTable.setText(rowNumbers, 0, textToAdd);
		rowNumbers++;
		scrollToBottom();
	}
	
	public void setRelativeHeight() {
		scrollPanel.setHeight( String.valueOf(Window.getClientHeight()-55) + "px");
		scrollToBottom();
	}
	
	public void scrollToBottom() {
		scrollPanel.scrollToBottom();
	}

	public void scrollToTop() {
		scrollPanel.scrollToTop();
	}
	
	public void clear() {
		int rowcount = flexTable.getRowCount();
		for (int i=rowcount; i!=0; i--)
			flexTable.removeRow(0);
	}
	
}
